package com.example.administrator.myapplication;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by Administrator on 2016/3/17 0017.
 */
public class MyAdapterImooc extends BaseAdapter{
    private  List<News> mList;
    private LayoutInflater mInflater;
    public MyAdapterImooc(Context context,List<News> list){
        mList = list;
        mInflater = LayoutInflater.from(context);
    }
    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //逗逼式
//        View view = mInflater.inflate(R.layout.item_list2,null);
//        ImageView imageView = (ImageView) view.findViewById(R.id.imageView1);
//        TextView title = (TextView) view.findViewById(R.id.tv_title);
//        TextView content = (TextView) view.findViewById(R.id.tv_content);
//
//        News news = mList.get(position);
//        imageView.setImageResource(news.getPicId());
//        title.setText(news.getTitle());
//        content.setText(news.getSummary());
//        return view;
        //普通式 利用ListeView 的缓存特性，如果没有缓存才创建新的View
        //但是findviewById依然会浪费大量的时间
//        if(convertView == null){
//            convertView = mInflater.inflate(R.layout.item_list2,null);
//        }
//        ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView1);
//        TextView title = (TextView) convertView.findViewById(R.id.tv_title);
//        TextView content = (TextView) convertView.findViewById(R.id.tv_content);
//
//        News news = mList.get(position);
//        imageView.setImageResource(news.getPicId());
//        title.setText(news.getTitle());
//        content.setText(news.getSummary());
//
//        return convertView;
        //文艺式 利用listView的缓存机制,同时避免重复findViewById
        //谷歌开发者在2013年ID大会上提供了一个ViewHolder去帮助我们优化BaseAdapter
        ViewHolder viewHolder;
        if(convertView == null){
            viewHolder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.item_list2,null);
            viewHolder.imageView = (CircleImageView) convertView.findViewById(R.id.imageView1);
            viewHolder.title = (TextView) convertView.findViewById(R.id.tv_title);
            viewHolder.content = (TextView) convertView.findViewById(R.id.tv_content);
            //convertView跟viewHolder关联
            convertView.setTag(viewHolder);
        }
        else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        News news = mList.get(position);
        //通过viewHolder赋值
        viewHolder.imageView.setImageResource(news.getPicId());
        viewHolder.title.setText(news.getTitle());
        viewHolder.content.setText(news.getSummary());



        return convertView;

    }
    class ViewHolder{
        public CircleImageView imageView;
        public TextView title;
        public TextView content;
    }
}
